package xju.rjxy.shop.mapper.impl;

import xju.rjxy.shop.mapper.CommunityMapper;
import xju.rjxy.shop.model.domain.CommunityPost;
import xju.rjxy.shop.common.utils.MyDbUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CommunityMapperImpl implements CommunityMapper {

    @Override
    public List<CommunityPost> getCommunityPosts() {
        List<CommunityPost> posts = new ArrayList<>();
        String sql = "SELECT cp.*, u.username FROM community_posts cp " +
                    "LEFT JOIN users u ON cp.user_id = u.id " +
                    "WHERE cp.flag = 2 AND cp.deleted = 0 " +
                    "ORDER BY cp.create_time DESC";
        
        try (Connection conn = MyDbUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql);
             ResultSet rs = ps.executeQuery()) {
            
            while (rs.next()) {
                CommunityPost post = new CommunityPost();
                post.setId(rs.getInt("id"));
                post.setUserId(rs.getInt("user_id"));
                post.setTitle(rs.getString("title"));
                post.setContent(rs.getString("content"));
                post.setImages(rs.getString("images"));
                post.setVideoUrl(rs.getString("video_url"));
                post.setFlag(rs.getInt("flag"));
                post.setDeleted(rs.getInt("deleted"));
                post.setCreateTime(rs.getTimestamp("create_time"));
                post.setUpdateTime(rs.getTimestamp("update_time"));
                post.setUsername(rs.getString("username"));
                posts.add(post);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return posts;
    }

    @Override
    public CommunityPost getPostById(int id) {
        String sql = "SELECT cp.*, u.username FROM community_posts cp " +
                    "LEFT JOIN users u ON cp.user_id = u.id " +
                    "WHERE cp.id = ? AND cp.flag = 2 AND cp.deleted = 0";
        
        try (Connection conn = MyDbUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            
            ps.setInt(1, id);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    CommunityPost post = new CommunityPost();
                    post.setId(rs.getInt("id"));
                    post.setUserId(rs.getInt("user_id"));
                    post.setTitle(rs.getString("title"));
                    post.setContent(rs.getString("content"));
                    post.setImages(rs.getString("images"));
                    post.setVideoUrl(rs.getString("video_url"));
                    post.setFlag(rs.getInt("flag"));
                    post.setDeleted(rs.getInt("deleted"));
                    post.setCreateTime(rs.getTimestamp("create_time"));
                    post.setUpdateTime(rs.getTimestamp("update_time"));
                    post.setUsername(rs.getString("username"));
                    return post;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int createPost(CommunityPost post) {
        String sql = "INSERT INTO community_posts (user_id, title, content, images, video_url, flag, deleted, create_time, update_time) " +
                    "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
        
        try (Connection conn = MyDbUtil.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            
            ps.setInt(1, post.getUserId());
            ps.setString(2, post.getTitle());
            ps.setString(3, post.getContent());
            ps.setString(4, post.getImages());
            ps.setString(5, post.getVideoUrl());
            ps.setInt(6, post.getFlag());
            ps.setInt(7, post.getDeleted());
            ps.setTimestamp(8, new Timestamp(post.getCreateTime().getTime()));
            ps.setTimestamp(9, new Timestamp(post.getUpdateTime().getTime()));
            
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
} 